package de.baumann.browser.activitys.center

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IWithdrawView
import de.baumann.browser.present.WithdrawPresenter
import de.baumann.browser.units.TimerUnit
import kotlinx.android.synthetic.main.activity_withdraw.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import java.text.DecimalFormat

/**
 * @author：
 * @date：2018/10/19
 * @discription: 提现
 */
@Deprecated("废弃")
class WithdrawActivity : BaseOdinActivity<IWithdrawView, WithdrawPresenter>(), IWithdrawView, View.OnClickListener {

    companion object {
        private const val LOADFACTOR = 0.08
    }
    private var withdrawAmount = 0
    private var realAmount = withdrawAmount * LOADFACTOR
    private var timerUnit: TimerUnit? = null

    override fun getTitleText(): String {
        return getString(R.string.withdraw)
    }

    override fun getLayout(): Int {
        return R.layout.activity_withdraw
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        btnWithdrawGetCode.setOnClickListener(this)
        btnConfirmWithdraw.setOnClickListener(this)
        edWithdrawAmount.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                setHandlingFee(charSequence.toString())
            }

            override fun afterTextChanged(editable: Editable) {

            }
        })
    }


    @SuppressLint("SetTextI18n")
    override fun initData() {
        getUserInfo()
        tvCurrentOver!!.text = intent.getDoubleExtra("odinOver", 0.0000).toString() + "ODIN"
    }

    private fun getUserInfo() {
        presenter.getEthAndPhone()
    }

    override fun createPresenter(): WithdrawPresenter? {
        return WithdrawPresenter()
    }

    override fun createView(): IWithdrawView? {
        return this
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == BINDETH && resultCode == Activity.RESULT_OK) {
            getUserInfo()
        }
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.btnWithdrawGetCode -> {
                if (timerUnit == null) {
                    timerUnit = TimerUnit(btnWithdrawGetCode)
                }
                timerUnit!!.startTime()
                presenter.getSmsCode()
            }
            R.id.btnConfirmWithdraw -> presenter.withdraw()
        }
    }

    override fun onError(errorMsg: String) {
        super.onError(errorMsg)
        timeEnd()
    }

    override fun onError(errorMsgId: Int) {
        super.onError(errorMsgId)
        timeEnd()
    }

    private fun timeEnd() {
        if (timerUnit != null)
            timerUnit?.endtTime()
    }

    override fun smsCodeSent() {
        showShortToast(getString(R.string.Verification_code_sent))
    }

    override fun withdrawSuccess() {
        showShortToast(getString(R.string.Successful_withdrawal))
        setResult(Activity.RESULT_OK)
        finish()
    }

    override fun setEthUrl(ethUrl: String?) {
        tvWithdrawEth!!.text = ethUrl
    }

    override fun setPhone(phone: String?) {
        tvWithdrawPhone!!.text = phone
    }

    @SuppressLint("SetTextI18n")
    private fun setRealAmount(realAmount: Double) {
        tvAccountEntryAmount!!.text = "$realAmount ODIN"
    }

    /**
     * 手续费
     */
    @SuppressLint("DefaultLocale")
    private fun setHandlingFee(amount: String) {
        if (TextUtils.isEmpty(amount)) {
            tvHandlingFee!!.text = ""
            tvAccountEntryAmount!!.text = ""
            withdrawAmount = 0
            return
        }

        this.withdrawAmount = Integer.valueOf(amount)
        val d = withdrawAmount * LOADFACTOR
        val df = DecimalFormat("#.00")
        tvHandlingFee!!.text = String.format("%s*%s=%s ODIN", amount, LOADFACTOR, df.format(d))
        this.realAmount = withdrawAmount - withdrawAmount * LOADFACTOR
        setRealAmount(realAmount)
    }


    override val phone: String
        get() = tvWithdrawPhone.text.trim().toString()

    override val smsCode: String
        get() = edWithdrawSmsCode.text.trim().toString()

    override val ethUrl: String
        get() = tvWithdrawEth.text.trim().toString()

    override val amount: String
        get() = withdrawAmount.toString()

}
